/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import com.opensymphony.xwork2.validator.annotations.DoubleRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
import com.paris5.master2.guitarmaster.bean.EcomUser;
import com.paris5.master2.guitarmaster.exception.EcomException;
import com.paris5.master2.guitarmaster.exception.EcomValidationException;
import com.paris5.master2.guitarmaster.service.ServiceCustomerRemote;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Leticia
 */
public class CrediterCompte extends EcomAction{
    private String password;
    private float count;

    @Override
    public String execute()
    {
        ServiceCustomerRemote serviceCustomerRemote = lookupServiceCustomerRemote();
        EcomUser ecomUser = (EcomUser) getFromSession("USER");
        if (password.equals(ecomUser.getPassword()))
        {
            try {
                serviceCustomerRemote.setAmount(ecomUser.getId(), count);
                setMessage("Montant crédité avec succès.");
                return SUCCESS;
            } catch (EcomValidationException ex) {
                Logger.getLogger(CrediterCompte.class.getName()).log(Level.SEVERE, null, ex);
            } catch (EcomException ex) {
                Logger.getLogger(CrediterCompte.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else
        {
            setMessage("Mot de passe erroné.");
            return INPUT;
        }
        return ERROR;
    }
    
    @RequiredStringValidator(message = "Mot de passe requis.")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @DoubleRangeFieldValidator(type = ValidatorType.SIMPLE, minInclusive = "1.0", message = "Veuillez saisir le montant à créditer (minimum 1€)")
    public float getCount() {
        return count;
    }

    public void setCount(float count) {
        this.count = count;
    }
}
